package demo2.reflect_250225;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Reflect1 {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        User user = new User("杨通", 12);
        Class<?> aClass = user.getClass();
        Field name = aClass.getDeclaredField("name");
        name.setAccessible(true);
        String nameVal = (String) name.get(user);
        System.out.println(nameVal);

        Method sayMethod = aClass.getDeclaredMethod("say");
        sayMethod.invoke(user);
    }

}

class User {
    private String name;
    private int age;
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void say() {
        System.out.println(this.name + " " + this.age);
    }
}
